package com.edu.medicalpro.mvp.presenter;

import android.util.Log;

import com.edu.medicalpro.bean.Doctor1;
import com.edu.medicalpro.bean.DoctorData;
import com.edu.medicalpro.bean.PatientResult;
import com.edu.medicalpro.bean.User;
import com.edu.medicalpro.bean.YIYuanJiesShao;
import com.edu.medicalpro.intercepter.Constant;
import com.edu.medicalpro.mvp.HomeContrace;
import com.edu.medicalpro.mvp.model.HomeModel;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class HomePresenter implements HomeContrace.IHomePresenter {

    private HomeModel model;
    private HomeContrace.IView mView;
    private HomeContrace.YiYuanJieShaoView yiYuanJieShaoView;
    public HomePresenter(HomeContrace.IView mHomeView) {
        model = new HomeModel();
        this.mView =mHomeView;
    }

    public HomePresenter(HomeContrace.YiYuanJieShaoView yiYuanJieShaoView) {
        model = new HomeModel();
        this.yiYuanJieShaoView = yiYuanJieShaoView;
    }

    @Override
    public void login(String name,String password) {
         model.login(name,password)
         .subscribeOn(Schedulers.io())
         .observeOn(AndroidSchedulers.mainThread())
         .subscribe(new Consumer<User>() {
             @Override
             public void accept(User user) throws Throwable {

                 mView.loginResult(user);
                 String token = user.getData().getToken();
                 Constant.TOKEN=token;

                 Log.e("TAGlll",user.toString());
             }
         }, new Consumer<Throwable>() {
             @Override
             public void accept(Throwable throwable) throws Throwable {
                 mView.LoginError(throwable);
             }
         });
    }

    @Override
    public void addPatient(Doctor1 doctor1) {
        model.addPatient(doctor1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<PatientResult>() {
                    @Override
                    public void accept(PatientResult user) throws Throwable {
                        Log.e("TAG",user.toString());
                        mView.Result(user);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                    }
                });
    }
    @Override
    public void getDoctor(int pageNum, int pageSize) {
        model.getDoctor(pageNum,pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DoctorData>() {
                    @Override
                    public void accept(DoctorData doctor) throws Throwable {
                        mView.DoctorResult(doctor);
                        Log.e("TAG",doctor.toString());
                        //   String token = user.getData().getToken();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        Log.e("TAG","Throwable");
                    }
                });
    }

    @Override
    public void getYYJS() {
        model.getYYJS()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<YIYuanJiesShao<YIYuanJiesShao.DeanInfoBean>>() {
                    @Override
                    public void accept(YIYuanJiesShao<YIYuanJiesShao.DeanInfoBean> deanInfoBeanYIYuanJiesShao) throws Throwable {
                        Log.e("TAG",deanInfoBeanYIYuanJiesShao.toString());
                        yiYuanJieShaoView.YiYuanJieShao(deanInfoBeanYIYuanJiesShao);
                    }
                });
    }


}
